package com.njtoyo.taxi.admin.service.business.operation_rule.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.cache.PrimaryCachePutter;
import com.njtoyo.taxi.admin.library.common.OperationRecordHelper;
import com.njtoyo.taxi.admin.mapper.master.entity.Company;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverRegionServiceArrangement;
import com.njtoyo.taxi.admin.mapper.master.entity.GeoDriverRegion;
import com.njtoyo.taxi.admin.mapper.master.enums.DriverRegionServiceArrangementCategory;
import com.njtoyo.taxi.admin.mapper.master.fields.operation_rule.QrCodeRate;
import com.njtoyo.taxi.admin.mapper.master.mapper.CompanyMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverRegionServiceArrangementMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.GeoDriverRegionMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.operation_rule.DriverServiceArrangementPresenter;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.AdminUserIdentityPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.DriverServiceArrangementWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.QrCodeCreateWrapper;
import com.njtoyo.taxi.admin.service.business.operation_rule.QrCodeService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static java.util.stream.Collectors.toList;

@Service
public class QrCodeServiceImpl implements QrCodeService {

    @Autowired
    private DriverRegionServiceArrangementMapper driverRegionServiceArrangementMapper;

    @Autowired
    private OperationRecordHelper operationRecordHelper;

    @Autowired
    private GeoDriverRegionMapper geoDriverRegionMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private PrimaryCachePutter primaryCachePutter;

    /**
     * 二维码列表
     *
     * @param adminUser
     * @param wrapper
     * @return
     */
    @Override
    public RestResult<IPage<DriverServiceArrangementPresenter>> page(AdminUser adminUser,
                                                                     DriverServiceArrangementWrapper wrapper) {
        wrapper.setServiceCategory(DriverRegionServiceArrangementCategory.qr_code);
        AdminUserIdentityPresenter identity = (AdminUserIdentityPresenter) adminUser.getIdentity();
        switch (adminUser.getAccountType()) {
            case canton:
            case company:
                wrapper.setDriverRegionIds(identity.getDriverRegionIds());
                break;
        }
        IPage<DriverServiceArrangementPresenter> res = driverRegionServiceArrangementMapper.selectPage(wrapper);
        return RestResult.success(res);
    }

    /**
     * 添加二维码
     *
     * @param adminUser
     * @param wrapper
     * @return
     */
    @Override
    public RestResult create(AdminUser adminUser, QrCodeCreateWrapper wrapper) {
        GeoDriverRegion driverRegion = geoDriverRegionMapper.selectById(wrapper.getDriverRegionId());
        if (Objects.isNull(driverRegion)) {
            return RestResult.failed("司机运营区不存在");
        }
        DriverRegionServiceArrangement arrangement = driverRegionServiceArrangementMapper
                .selectByCategoryAndDriverRegionId(DriverRegionServiceArrangementCategory.qr_code, wrapper.getDriverRegionId());
        if (Objects.nonNull(arrangement)) {
            return RestResult.failed("配置已存在");
        }
        QrCodeRate detailSpecification = wrapper.getDetailSpecification();
        List<Long> companyIds = detailSpecification.getCodeCategory().stream().map(QrCodeRate.Separate::getCompanyId).collect(toList());
        if (Objects.nonNull(companyIds)) {
            QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", companyIds)
                    .eq("driver_region_id", wrapper.getDriverRegionId());
            Integer count = companyMapper.selectCount(queryWrapper);
            if (count != detailSpecification.getCodeCategory().size()) {
                return RestResult.failed("存在非法公司或者非本地区公司");
            }
        }
        //二维码
        QrCodeRate qrCodeRate =
                wrapper.getDetailSpecification();
        if (!Objects.nonNull(qrCodeRate.getCodeCategory())) {
            return RestResult.failed("客服的二维码不能为空");
        }

        DriverRegionServiceArrangement arrangement1 = new DriverRegionServiceArrangement();
        arrangement1.setDriverRegionId(wrapper.getDriverRegionId())
                .setIsOpen(wrapper.getIsOpen())
                .setServiceCategory(DriverRegionServiceArrangementCategory.qr_code)
                .setDetailSpecification(JSONUtil.toJsonStr(wrapper.getDetailSpecification()));
        driverRegionServiceArrangementMapper.insert(arrangement1);
        //添加缓存
        primaryCachePutter.deleteDriverServiceArrangement();
        operationRecordHelper.addBusinessLog(String.format("新增二维码 [%s]", arrangement1));
        return RestResult.success();
    }
}
